
const { find,add,update,singlePerson} = require('../model/personModel')


//获取
const getPerson=async(req,res,next)=>{
    console.log(req.query,999)
    const result = await find(req.query);
    
    if (result.length||result.length===0) {
        res.send({ httpStatus: 200,  total: result.length, data: result })
    } else {
        res.send({ httpStatus: 400,  msg: '查询出错' })
    }
}


//新增
const writePerson=async (req,res,next)=>{
    const result=await add(req.body)
    if(result){
        res.send({httpStatus:200,msg:'增加成功',data:result})
    }else{
        res.send({httpStatus:400,msg:'增加出错'})
    }
}



//修改
const updatePerson = async (req, res) => {
    const { createBy,name } = req.body;
    const query = { createBy,name };
    delete req.body._id;
    delete req.body.createBy;
    delete req.body.imageUrl;
    const updated = req.body;
    const result = await update(query, updated)
    if (result.nModified !== 0) {
        res.send({ httpStatus: 200,  msg: '修改成功' })
    } else {
        res.send({ httpStatus: 400,  msg: '修改出错' })
    }

}
const uploadAvatar=async (req,res)=>{
    console.log(req.body,9999999999)
    let query={createBy:req.body.createBy,name:req.body.name}
    let updated={$set:{imageUrl:req.body.imageUrl}}

    let result =await update(query,updated);
    if(result.n){
        res.send({httpStatus:200,msg:'图片上传成功'})
    }else{
        res.send({httpStatus:400,msg:'图片上传失败'})
    }
}
module.exports = {
    getPerson,
    writePerson,
    updatePerson,
    uploadAvatar,
}